package com.xiaomi.controller;

import com.xiaomi.constant.MessageConstant;
import com.xiaomi.dto.VehicleDTO;
import com.xiaomi.entity.Vehicle;
import com.xiaomi.exception.BusinessException;
import com.xiaomi.result.Result;
import com.xiaomi.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/vehicle")
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    /**
     * 添加车辆信息
     * @param vehicle
     * @return
     */
    @PostMapping("/addVehicle")
    public Result<Vehicle> add(@RequestBody Vehicle vehicle){
        log.info("添加车辆信息，参数：{}",vehicle);
        Vehicle newVehicle = vehicleService.insert(vehicle);
        return Result.success(MessageConstant.ADD_VEHICLE_SUCCESS,newVehicle);
    }

    /**
     * 根据frameNumber查询车辆信息
     * @param frameNumber
     * @return
     */
    @GetMapping("/getByVid/{frameNumber}")
    public Result<Vehicle> getByVid(@PathVariable Long frameNumber) {
        log.info("根据车架编号查询车辆信息，参数：{}",frameNumber);
        return Result.success(MessageConstant.QUERY_VEHICLE_BY_ID_SUCCESS,vehicleService.getById(frameNumber));
    }

    /**
     * 更新车辆信息
     * @param vehicleDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody VehicleDTO vehicleDTO){
        log.info("更新车辆信息，参数：{}",vehicleDTO);
        vehicleService.update(vehicleDTO);
        return Result.success(MessageConstant.UPDATE_VEHICLE_SUCCESS);
    }

    /**
     * 根据frameNumber删除车辆信息
     * @param frameNumber
     * @return
     */
    @DeleteMapping("/deleteById/{frameNumber}")
    public Result deleteById(@PathVariable Long frameNumber){
        log.info("根据frameNumber删除车辆信息，参数：{}",frameNumber);
        vehicleService.deleteById(frameNumber);
        return Result.success(MessageConstant.DELETE_VEHICLE_SUCCESS);
    }

    /**
     * 查询所有车辆信息
     * @return
     */
    @GetMapping("/getAllVehicles")
    public Result<List<Vehicle>> getByVin() {
        log.info("查询所有车辆信息");
        return Result.success(MessageConstant.QUERY_VEHICLE_BY_ID_SUCCESS,vehicleService.getAllVehicles());
    }
}
